{% extends 'app_doc/manage_base.html' %}
{% load static %}
{% block title %}文集设置{% endblock %}
{% block content %}
    <link href="{% static 'tagsInput/tagsinput.css' %}" rel="stylesheet" type="text/css"/>
    <div class="layui-row" style="margin-bottom: 10px;padding-left:15px;">
        <span class="layui-breadcrumb" lay-separator=">">
          <a href="{% url 'manage_project' %}">文集管理</a>
          <a><cite>文集设置</cite></a>
        </span>
    </div>
    <div class="layui-card-header" style="margin-bottom: 10px;">

      <div class="layui-row">
          <span style="font-size:18px;">文集：{{pro.name}}
          </span>
      </div>
  </div>

    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">基础信息</li>
            <li>权限配置</li>
            <li>下载配置</li>
            <li>管理控制</li>
        </ul>
        <div class="layui-tab-content">
            <!-- 文集基础信息配置 -->
            <div class="layui-tab-item layui-show">
                <div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">文集名称</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" id="pname" style="margin-bottom:10px;" placeholder="输入文集名" required  lay-verify="required" value="{{pro.name}}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">文集简介</label>
                        <div class="layui-input-block">
                            <textarea name="desc" id="desc" placeholder="输入文集简介" class="layui-textarea">{{pro.intro}}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item" style="margin-top: 10px;">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-primary layui-btn-sm" id="modify-project-btn"><i class="layui-icon layui-icon-edit"></i>立即修改</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 文集权限配置 -->
            <div class="layui-tab-item">
                <div class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">浏览权限</label>
                        <div class="layui-input-block">
                            <input type="radio" name="role" value="0" title="公开" {% if pro.role == 0 %} checked {%endif%} lay-filter="role">
                            <input type="radio" name="role" value="1" title="私密" {% if pro.role == 1 %} checked {%endif%} lay-filter="role">
                            <input type="radio" name="role" value="2" title="指定用户可见" {% if pro.role == 2 %} checked {%endif%} lay-filter="role">
                            <input type="radio" name="role" value="3" title="访问码可见" {% if pro.role == 3 %} checked {%endif%} lay-filter="role">
                        </div>
                    </div>
        
                    <div class="layui-form-item" style="{% if pro.role == 3 %}{% else %}display:none;{% endif %}" id="role-pwd">
                        <label class="layui-form-label">访问码</label>
                        <div class="layui-input-inline">
                          <input type="text" name="viewcode" id="viewcode" placeholder="请输入访问码" autocomplete="off" class="layui-input" value="{% if pro.role_value != None %}{{pro.role_value}}{% endif %}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">不少于4位数</div>
                    </div>
        
                    <div class="layui-form-item" style="{% if pro.role == 2 %}{% else %}display:none;{% endif %}" id="role-user">
                        <label class="layui-form-label">允许用户</label>
                        <div class="layui-input-block">
                            <div class="tagsinput-primary form-group">
                                <input name="tagsinput" id="tagsinputval" class="tagsinput" data-role="tagsinput" value="{% if pro.role_value != None %}{{pro.role_value}}{% endif %}" placeholder="请输入用户名，回车输入多个用户">
                            </div>
                        </div>
                    </div>
        
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                          <button class="layui-btn layui-btn-primary layui-btn-sm" id="modify-project-role">修改权限</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 下载和导出 -->
            <div class="layui-tab-item">
                <div class="layui-form">
                    {% load project_filter %}
                    <div class="layui-form-item">
                        <blockquote class="layui-elem-quote">注意：开启某类型文件下载后，请先点击“生成或更新XXX文件”，文集文档中如果包含公式、流程图、时序图、脑图等内容，将会延长生成时间，请耐心等待</blockquote>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">EPUB下载</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="download_epub"
                               {% if pro.id|report_status_epub == 1 %} checked {% endif %}
                               lay-skin="switch" lay-text="允许|禁止">
                               <!-- 判断后台是否开启导出，如果开启，则显示 -->
                               {% if enable_project_report %}
                                    <a href="javascript:void(0);" onclick="reportFile('{{pro.id}}','epub')"><i class="layui-icon layui-icon-refresh"></i><u>生成或更新EPUB文件</u></a>
                                    {% if project_files %}
                                        {% for file in project_files %}
                                            {% if file.file_type == 'epub' %}
                                                | <a href="{{file.file_path}}" target="_blank"><i class="layui-icon layui-icon-download-circle"></i><u>下载文集EPUB文件</u></a>
                                            {% else %}
                                                
                                            {% endif %}
                                        {% endfor %}
                                    {% else %}
                                    | <span style="color: #ff213b;">未生成文集导出文件</span>
                                    {% endif %}
                                {% endif %}
                        </div>
                    </div>
                    <hr>
                    <div class="layui-form-item">
                        <label class="layui-form-label">PDF下载</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="download_pdf"
                               {% if pro.id|report_status_pdf == 1 %} checked {%endif%}
                               lay-skin="switch" lay-text="允许|禁止">
                               <!-- 判断后台是否开启导出，如果开启，则显示 -->
                               {% if enable_project_report %}
                                    <a href="javascript:void(0);" onclick="reportFile('{{pro.id}}','pdf')" style=""><i class="layui-icon layui-icon-refresh"></i><u>生成或更新PDF文件</u></a>
                                    {% if project_files %}
                                        {% for file in project_files %}
                                            {% if file.file_type == 'pdf' %}
                                                | <a href="{{file.file_path}}" target="_blank"><i class="layui-icon layui-icon-download-circle"></i><u>下载文集PDF文件</u></a>
                                            {% else %}
                                                
                                            {% endif %}
                                        {% endfor %}
                                    {% else %}
                                    | <span style="color: #ff213b;">未生成文集导出文件</span>
                                    {% endif %}
                                {% endif %}
                        </div>
                    </div>
                    <hr>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                          <button class="layui-btn layui-btn-primary layui-btn-sm" id="modify-project-download">保存文集导出配置</button>
                        </div>
                    </div>
                    
                </div>
            </div>
            <!-- 文集删除和转让 -->
            <div class="layui-tab-item">
                <div class="layui-row">
                    <div style="float: left;">
                        <strong>导出文集</strong>
                        <p>将文集内的文档以Markdown（.md）的形式导出为zip压缩文件</p>
                    </div>
                    <button class="layui-btn layui-btn-primary" style="float: right;" id="export-project">导出文集</button>
                </div>
                <hr>
                <div class="layui-row">
                    <div style="float: left;">
                        <strong>转让文集</strong>
                        <p>将文集的管理权限和文集下自己创建的文档转让给站点的其他用户</p>
                    </div>
                    <a class="layui-btn layui-btn-primary" style="float: right;" id="transfer-project" href="{% url 'manage_pro_transfer' pro.id %}">转让文集</a>
                </div>
                <hr>
                <div class="layui-row">
                    <div style="float: left;">
                        <strong>删除文集</strong>
                        <p>删除文集及文集下的所有文档</p>
                    </div>
                    <button class="layui-btn layui-btn-warm" style="float: right;" id="delete-project">删除文集</button>
                </div>
            </div>
            
        </div>
    </div>
{% endblock %}
{% block custom_script %}
<script src="{% static '/tagsInput/tagsinput.js' %}" type="text/javascript" charset="utf-8"></script>
<script>
    $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
        });
    var form = layui.form;
    //监听单选事件
    form.on('radio(role)', function(data){
      console.log(data.elem); //得到radio原始DOM对象
      console.log(data.value); //被点击的radio的value值
      if(data.value in [0,1]){
            $("#role-pwd").css("display","none");
            $("#role-user").css("display","none");
      }else if(data.value == 2){
            $("#role-user").css("display","block");
            $("#role-pwd").css("display","none");
      }else if(data.value == 3){
            $("#role-user").css("display","none");
            $("#role-pwd").css("display","block");
      }
    });
    //修改文集基础信息
    modifyProject = function(pro_id,pro_name,pro_intro){
        layer.load(1);
        var project_data = {
            'pro_id':'{{pro.id}}',
            'name':$("#pname").val(),
            'desc':$("#desc").val(),
        }
        $.post("{% url 'modify_project' %}",project_data,function(r){
            layer.closeAll('loading'); //关闭loading
            if(r.status){
                //修改成功
                //window.location.reload();
                layer.msg("修改成功")
            }else{
                //修改失败，提示
                // console.log(r)
                layer.msg(r.data)
            }
        })
    };
    $("#modify-project-btn").click(function(){
        modifyProject();
    });
    // 修改文集权限
    modifyProjectRole = function(){
        layer.load(1);
        var role_data = {
            'role':$("input[name='role']:checked").val(),
            'tagsinput':$("#tagsinputval").val(),
            'viewcode':$("#viewcode").val(),
        }
        $.post("{% url 'modify_pro_role' pro.id %}",role_data,function(r){
            layer.closeAll('loading'); //关闭loading
            if(r.status){
                //修改成功
                //window.location.reload();
                layer.msg("修改成功")
            }else{
                //修改失败，提示
                // console.log(r)
                layer.msg(r.data)
            }
        })
    };
    $('#modify-project-role').click(function(){
        modifyProjectRole();
    });
    // 修改文集导出配置
    modifyProjectDownload = function(){
        layer.load(1);
        var download_data = {
            'download_epub':$("input[name='download_epub']:checked").val(),
            'download_pdf':$("input[name='download_pdf']:checked").val(),
        }
        $.post("{% url 'modify_pro_download' pro.id %}",download_data,function(r){
            layer.closeAll('loading'); //关闭loading
            if(r.status){
                //修改成功
                layer.msg("修改成功")
            }else{
                //修改失败，提示
                // console.log(r)
                layer.msg(r.data)
            }
        })
    };
    $('#modify-project-download').click(function(){
        modifyProjectDownload();
    });
    // 生成文集文件
    reportFile = function(pro_id,types){
        layer.load(1)
        var data = {'pro_id':pro_id,'types':types};
        $.post("{% url 'genera_project_file' %}",data,function(r){
            layer.closeAll('loading');
            if(r.status){
                layer.msg("生成完成")
                window.location.reload();
            }else{
                layer.msg("生成出错，请稍后重试")
            }
        })
    }
    // 删除文集
    delProject = function(pro_id){
        layer.open({
            type:1,
            title:'删除文集',
            area:'300px;',
            id:'delPro',//配置ID
            content:'<div style="margin-left:10px;">警告：此操作将删除文集及文集下所有文档！</div>',
            btn:['确定','取消'], //添加按钮
            btnAlign:'c', //按钮居中
            yes:function (index,layero) {
                layer.load(1);
                data = {
                    'pro_id':pro_id,
                }
                $.post("{% url 'del_project' %}",data,function(r){
                    layer.closeAll('loading'); //关闭loading
                    if(r.status){
                        //删除成功
                        layer.msg('删除成功，即将返回文集列表……',function(){
                            window.location.href = "{% url 'manage_project' %}"
                        })
                        //layer.close(index)
                    }else{
                        //删除失败，提示
                        console.log(r)
                        layer.msg(r.data)
                    }
                })
            },
        });
    };
    $('#delete-project').click(function(){
        delProject('{{pro.id}}')
    })
    //导出MD
    reportMd = function(pro_id){
        layer.open({
            type:1,
            title:"导出文集",
            area:"300px",
            id:"reportMd",
            content:'<div style="margin-left:10px;">将此文集下所有文档导出为MD文件并打包？</div>',
            btn:['确定','取消'], //添加按钮
            btnAlign:'c', //按钮居中
            yes:function (index,layero) {
                var load = layer.load()
                data = {
                    'project_id':pro_id,
                }
                $.post("{% url 'report_md' %}",data,function(r){
                    layer.closeAll('loading'); //关闭loading
                    if(r.status){
                        //导出成功
                        //文件下载提示
                        downloadMd(r.data)
                    }else{
                        //导出失败，提示
                        // console.log(r)
                        layer.msg(r.data)
                    }
                    layer.close(load)
                })
            },
        })
    };
    $("#export-project").click(function(){
        reportMd('{{pro.id}}');
    });
    //下载文件弹出框
    downloadMd = function(download_link){
        layer.open({
            type:1,
            title:"下载导出文档",
            area:"300px",
            id:"downloadMd",
            content:'<p style="text-align:center;color:red;">请尽快下载，避免失效！</p><br><a class="layui-btn layui-btn-fluid download-md-link" href="'+ download_link + '" download="mrdoc_report_md.zip" >点击下载文件(zip)</a>',
            //btn:['确定','取消'], //添加按钮
            //btnAlign:'c', //按钮居中
            success: function (layero, index) {
                $(layero).find('.download-md-link').click(function () {
                    layer.closeAll();
                })
        }
        })
    };
</script>
{% endblock %}